#!/bin/bash -

OK=0
WARNING=1
UNKNOWN=2
CRITICAL=3

STATUS=$OK

PATH="/bin:/sbin:/usr/bin:/usr/sbin"

function send_mail(){
	title="$2"
	content="$1"
	echo "$content" | mutt -s "$title" biyujiang@ihep.ac.cn lihaibo@ihep.ac.cn
}

port_file="/etc/xrootd/port.lst"
if [[ ! -f $port_file ]]; then
	STATUS=$CRITICAL
	echo "$(hostname -s): No config file $port_file"
	exit $STATUS
fi

while read exp port;do
	health=$(timeout -k 5 5 redis-cli -p $port quarkdb-health|awk '/NODE-HEALTH/ {print $2}')
	if [[ $health != "GREEN" ]]; then
		STATUS=$CRITICAL
		echo "$(hostname): QuarkDB for $exp is down!"
	fi
done < $port_file

if [[ $STATUS == "$OK" ]]; then
	echo "$(hostname): QuarkDB is OK"
fi

exit $STATUS
